<?php

namespace app\load\model\work;

use app\load\loadModel\AdminModel;
use app\load\loadModel\WorksModel;
use think\db\BaseQuery;
use think\db\exception\DataNotFoundException;
use think\db\exception\DbException;
use think\db\exception\ModelNotFoundException;
use think\Model;
use think\Paginator;

/**
 * 日报表
 */
class WorkModel extends Model
{
    protected $name = 'work';

    /**
     * 列表
     * @param $where
     * @param $filed
     * @param $limit
     * @param $page
     * @return Paginator
     * @throws DbException
     */
    public function list($where, $filed, $limit, $page): Paginator
    {

        return $this->db()
            ->field($filed)
            ->where($where)
            ->filter(function ($data) {
                $data['accept_admin_list'] = AdminModel::Admin()->where('id', 'in', explode(',', $data['type_user']))
                    ->column('name', 'id');
                $data['create_admin_name'] = AdminModel::Admin()->where('id', $data['admin_id'])->value('name');
                $data['is_read'] = 1;
                if ($data['admin_id'] != JWT_UID) {
                    $data['is_read'] = WorksModel::WorkRecord()
                        ->where('wid', $data['id'])
                        ->where('to_uid', JWT_UID)->value('send_time') > 0 ? 1 : 0;
                }
            })->paginate([
                'var_page' => $page, //分页变量
                'list_rows' => $limit //每页数量
            ]);
    }

    /**
     * 详情
     * @param array $where
     * @param string $filed
     * @return array|mixed|BaseQuery|Model|null
     * @throws DbException
     * @throws DataNotFoundException
     * @throws ModelNotFoundException
     */
    public function info(array $where, string $filed)
    {
        return $this->db()
            ->field($filed)
            ->where($where)
            ->filter(function ($data) {
                $data['accept_admin_list'] = AdminModel::Admin()->where('id', 'in', explode(',', $data['type_user']))
                    ->column('name', 'id');
                $data['create_admin_name'] = AdminModel::Admin()->where('id', $data['admin_id'])->value('name');
                $data['is_read'] = 1;
                if ($data['admin_id'] != JWT_UID) {
                    $data['is_read'] = WorksModel::WorkRecord()
                        ->where('wid', $data['id'])
                        ->where('to_uid', JWT_UID)->value('send_time') > 0 ? 1 : 0;
                }
            })->find();

    }
}